home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Objects / abstract.c next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  27.8 KB  |  1,470 lines

  1. /* Abstract Object Interface (many thanks to Jim Fulton) */
  2.  
  3. #include "Python.h"
  4. #include <ctype.h>
  5.  
  6. /* Shorthands to return certain errors */
  7.  
  8. static PyObject *
  9. type_error(const char *msg)
  10. {
  11.     PyErr_SetString(PyExc_TypeError, msg);
  12.     return NULL;
  13. }
  14.  
  15. static PyObject *
  16. null_error(void)
  17. {
  18.     if (!PyErr_Occurred())
  19.         PyErr_SetString(PyExc_SystemError,
  20.                 "null argument to internal routine");
  21.     return NULL;
  22. }
  23.  
  24. /* Operations on any object */
  25.  
  26. int
  27. PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
  28. {
  29.     int r;
  30.  
  31.     if (o1 == NULL || o2 == NULL) {
  32.         null_error();
  33.         return -1;
  34.     }
  35.     r = PyObject_Compare(o1, o2);
  36.     if (PyErr_Occurred())
  37.         return -1;
  38.     *result = r;
  39.     return 0;
  40. }
  41.  
  42. PyObject *
  43. PyObject_Type(PyObject *o)
  44. {
  45.     PyObject *v;
  46.  
  47.     if (o == NULL)
  48.         return null_error();
  49.     v = (PyObject *)o->ob_type;
  50.     Py_INCREF(v);
  51.     return v;
  52. }
  53.  
  54. int
  55. PyObject_Size(PyObject *o)
  56. {
  57.     PySequenceMethods *m;
  58.  
  59.     if (o == NULL) {
  60.         null_error();
  61.         return -1;
  62.     }
  63.  
  64.     m = o->ob_type->tp_as_sequence;
  65.     if (m && m->sq_length)
  66.         return m->sq_length(o);
  67.  
  68.     return PyMapping_Size(o);
  69. }
  70.  
  71. #undef PyObject_Length
  72. int
  73. PyObject_Length(PyObject *o)
  74. {
  75.     return PyObject_Size(o);
  76. }
  77. #define PyObject_Length PyObject_Size
  78.  
  79. PyObject *
  80. PyObject_GetItem(PyObject *o, PyObject *key)
  81. {
  82.     PyMappingMethods *m;
  83.  
  84.     if (o == NULL || key == NULL)
  85.         return null_error();
  86.  
  87.     m = o->ob_type->tp_as_mapping;
  88.     if (m && m->mp_subscript)
  89.         return m->mp_subscript(o, key);
  90.  
  91.     if (o->ob_type->tp_as_sequence) {
  92.         if (PyInt_Check(key))
  93.             return PySequence_GetItem(o, PyInt_AsLong(key));
  94.         else if (PyLong_Check(key)) {
  95.             long key_value = PyLong_AsLong(key);
  96.             if (key_value == -1 && PyErr_Occurred())
  97.                 return NULL;
  98.             return PySequence_GetItem(o, key_value);
  99.         }
  100.         return type_error("sequence index must be integer");
  101.     }
  102.  
  103.     return type_error("unsubscriptable object");
  104. }
  105.  
  106. int
  107. PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
  108. {
  109.     PyMappingMethods *m;
  110.  
  111.     if (o == NULL || key == NULL || value == NULL) {
  112.         null_error();
  113.         return -1;
  114.     }
  115.     m = o->ob_type->tp_as_mapping;
  116.     if (m && m->mp_ass_subscript)
  117.         return m->mp_ass_subscript(o, key, value);
  118.  
  119.     if (o->ob_type->tp_as_sequence) {
  120.         if (PyInt_Check(key))
  121.             return PySequence_SetItem(o, PyInt_AsLong(key), value);
  122.         else if (PyLong_Check(key)) {
  123.             long key_value = PyLong_AsLong(key);
  124.             if (key_value == -1 && PyErr_Occurred())
  125.                 return -1;
  126.             return PySequence_SetItem(o, key_value, value);
  127.         }
  128.         type_error("sequence index must be integer");
  129.         return -1;
  130.     }
  131.  
  132.     type_error("object does not support item assignment");
  133.     return -1;
  134. }
  135.  
  136. int
  137. PyObject_DelItem(PyObject *o, PyObject *key)
  138. {
  139.     PyMappingMethods *m;
  140.  
  141.     if (o == NULL || key == NULL) {
  142.         null_error();
  143.         return -1;
  144.     }
  145.     m = o->ob_type->tp_as_mapping;
  146.     if (m && m->mp_ass_subscript)
  147.         return m->mp_ass_subscript(o, key, (PyObject*)NULL);
  148.  
  149.     if (o->ob_type->tp_as_sequence) {
  150.         if (PyInt_Check(key))
  151.             return PySequence_DelItem(o, PyInt_AsLong(key));
  152.         else if (PyLong_Check(key)) {
  153.             long key_value = PyLong_AsLong(key);
  154.             if (key_value == -1 && PyErr_Occurred())
  155.                 return -1;
  156.             return PySequence_DelItem(o, key_value);
  157.         }
  158.         type_error("sequence index must be integer");
  159.         return -1;
  160.     }
  161.  
  162.     type_error("object does not support item deletion");
  163.     return -1;
  164. }
  165.  
  166. int PyObject_AsCharBuffer(PyObject *obj,
  167.               const char **buffer,
  168.               int *buffer_len)
  169. {
  170.     PyBufferProcs *pb;
  171.     const char *pp;
  172.     int len;
  173.  
  174.     if (obj == NULL || buffer == NULL || buffer_len == NULL) {
  175.         null_error();
  176.         return -1;
  177.     }
  178.     pb = obj->ob_type->tp_as_buffer;
  179.     if ( pb == NULL ||
  180.          pb->bf_getcharbuffer == NULL ||
  181.          pb->bf_getsegcount == NULL ) {
  182.         PyErr_SetString(PyExc_TypeError,
  183.                 "expected a character buffer object");
  184.         goto onError;
  185.     }
  186.     if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
  187.         PyErr_SetString(PyExc_TypeError,
  188.                 "expected a single-segment buffer object");
  189.         goto onError;
  190.     }
  191.     len = (*pb->bf_getcharbuffer)(obj,0,&pp);
  192.     if (len < 0)
  193.         goto onError;
  194.     *buffer = pp;
  195.     *buffer_len = len;
  196.     return 0;
  197.  
  198.  onError:
  199.     return -1;
  200. }
  201.  
  202. int PyObject_AsReadBuffer(PyObject *obj,
  203.               const void **buffer,
  204.               int *buffer_len)
  205. {
  206.     PyBufferProcs *pb;
  207.     void *pp;
  208.     int len;
  209.  
  210.     if (obj == NULL || buffer == NULL || buffer_len == NULL) {
  211.         null_error();
  212.         return -1;
  213.     }
  214.     pb = obj->ob_type->tp_as_buffer;
  215.     if ( pb == NULL ||
  216.          pb->bf_getreadbuffer == NULL ||
  217.          pb->bf_getsegcount == NULL ) {
  218.         PyErr_SetString(PyExc_TypeError,
  219.                 "expected a readable buffer object");
  220.         goto onError;
  221.     }
  222.     if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
  223.         PyErr_SetString(PyExc_TypeError,
  224.                 "expected a single-segment buffer object");
  225.         goto onError;
  226.     }
  227.     len = (*pb->bf_getreadbuffer)(obj,0,&pp);
  228.     if (len < 0)
  229.         goto onError;
  230.     *buffer = pp;
  231.     *buffer_len = len;
  232.     return 0;
  233.  
  234.  onError:
  235.     return -1;
  236. }
  237.  
  238. int PyObject_AsWriteBuffer(PyObject *obj,
  239.                void **buffer,
  240.                int *buffer_len)
  241. {
  242.     PyBufferProcs *pb;
  243.     void*pp;
  244.     int len;
  245.  
  246.     if (obj == NULL || buffer == NULL || buffer_len == NULL) {
  247.         null_error();
  248.         return -1;
  249.     }
  250.     pb = obj->ob_type->tp_as_buffer;
  251.     if ( pb == NULL ||
  252.          pb->bf_getwritebuffer == NULL ||
  253.          pb->bf_getsegcount == NULL ) {
  254.         PyErr_SetString(PyExc_TypeError,
  255.                 "expected a writeable buffer object");
  256.         goto onError;
  257.     }
  258.     if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
  259.         PyErr_SetString(PyExc_TypeError,
  260.                 "expected a single-segment buffer object");
  261.         goto onError;
  262.     }
  263.     len = (*pb->bf_getwritebuffer)(obj,0,&pp);
  264.     if (len < 0)
  265.         goto onError;
  266.     *buffer = pp;
  267.     *buffer_len = len;
  268.     return 0;
  269.  
  270.  onError:
  271.     return -1;
  272. }
  273.  
  274. /* Operations on numbers */
  275.  
  276. int
  277. PyNumber_Check(PyObject *o)
  278. {
  279.     return o && o->ob_type->tp_as_number;
  280. }
  281.  
  282. /* Binary operators */
  283.  
  284. #define BINOP(v, w, opname, ropname, thisfunc) \
  285.     if (PyInstance_Check(v) || PyInstance_Check(w)) \
  286.         return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
  287.  
  288. PyObject *
  289. PyNumber_Or(PyObject *v, PyObject *w)
  290. {
  291.     BINOP(v, w, "__or__", "__ror__", PyNumber_Or);
  292.     if (v->ob_type->tp_as_number != NULL) {
  293.         PyObject *x = NULL;
  294.         PyObject * (*f)(PyObject *, PyObject *);
  295.         if (PyNumber_Coerce(&v, &w) != 0)
  296.             return NULL;
  297.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  298.             x = (*f)(v, w);
  299.         Py_DECREF(v);
  300.         Py_DECREF(w);
  301.         if (f != NULL)
  302.             return x;
  303.     }
  304.     return type_error("bad operand type(s) for |");
  305. }
  306.  
  307. PyObject *
  308. PyNumber_Xor(PyObject *v, PyObject *w)
  309. {
  310.     BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor);
  311.     if (v->ob_type->tp_as_number != NULL) {
  312.         PyObject *x = NULL;
  313.         PyObject * (*f)(PyObject *, PyObject *);
  314.         if (PyNumber_Coerce(&v, &w) != 0)
  315.             return NULL;
  316.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  317.             x = (*f)(v, w);
  318.         Py_DECREF(v);
  319.         Py_DECREF(w);
  320.         if (f != NULL)
  321.             return x;
  322.     }
  323.     return type_error("bad operand type(s) for ^");
  324. }
  325.  
  326. PyObject *
  327. PyNumber_And(PyObject *v, PyObject *w)
  328. {
  329.     BINOP(v, w, "__and__", "__rand__", PyNumber_And);
  330.     if (v->ob_type->tp_as_number != NULL) {
  331.         PyObject *x = NULL;
  332.         PyObject * (*f)(PyObject *, PyObject *);
  333.         if (PyNumber_Coerce(&v, &w) != 0)
  334.             return NULL;
  335.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  336.             x = (*f)(v, w);
  337.         Py_DECREF(v);
  338.         Py_DECREF(w);
  339.         if (f != NULL)
  340.             return x;
  341.     }
  342.     return type_error("bad operand type(s) for &");
  343. }
  344.  
  345. PyObject *
  346. PyNumber_Lshift(PyObject *v, PyObject *w)
  347. {
  348.     BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift);
  349.     if (v->ob_type->tp_as_number != NULL) {
  350.         PyObject *x = NULL;
  351.         PyObject * (*f)(PyObject *, PyObject *);
  352.         if (PyNumber_Coerce(&v, &w) != 0)
  353.             return NULL;
  354.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  355.             x = (*f)(v, w);
  356.         Py_DECREF(v);
  357.         Py_DECREF(w);
  358.         if (f != NULL)
  359.             return x;
  360.     }
  361.     return type_error("bad operand type(s) for <<");
  362. }
  363.  
  364. PyObject *
  365. PyNumber_Rshift(PyObject *v, PyObject *w)
  366. {
  367.     BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift);
  368.     if (v->ob_type->tp_as_number != NULL) {
  369.         PyObject *x = NULL;
  370.         PyObject * (*f)(PyObject *, PyObject *);
  371.         if (PyNumber_Coerce(&v, &w) != 0)
  372.             return NULL;
  373.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  374.             x = (*f)(v, w);
  375.         Py_DECREF(v);
  376.         Py_DECREF(w);
  377.         if (f != NULL)
  378.             return x;
  379.     }
  380.     return type_error("bad operand type(s) for >>");
  381. }
  382.  
  383. PyObject *
  384. PyNumber_Add(PyObject *v, PyObject *w)
  385. {
  386.     PySequenceMethods *m;
  387.  
  388.     BINOP(v, w, "__add__", "__radd__", PyNumber_Add);
  389.     m = v->ob_type->tp_as_sequence;
  390.     if (m && m->sq_concat)
  391.         return (*m->sq_concat)(v, w);
  392.     else if (v->ob_type->tp_as_number != NULL) {
  393.         PyObject *x = NULL;
  394.         PyObject * (*f)(PyObject *, PyObject *);
  395.         if (PyNumber_Coerce(&v, &w) != 0)
  396.             return NULL;
  397.         if ((f = v->ob_type->tp_as_number->nb_add) != NULL)
  398.             x = (*f)(v, w);
  399.         Py_DECREF(v);
  400.         Py_DECREF(w);
  401.         if (f != NULL)
  402.             return x;
  403.     }
  404.     return type_error("bad operand type(s) for +");
  405. }
  406.  
  407. PyObject *
  408. PyNumber_Subtract(PyObject *v, PyObject *w)
  409. {
  410.     BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract);
  411.     if (v->ob_type->tp_as_number != NULL) {
  412.         PyObject *x = NULL;
  413.         PyObject * (*f)(PyObject *, PyObject *);
  414.         if (PyNumber_Coerce(&v, &w) != 0)
  415.             return NULL;
  416.         if ((f = v->ob_type->tp_as_number->nb_subtract) != NULL)
  417.             x = (*f)(v, w);
  418.         Py_DECREF(v);
  419.         Py_DECREF(w);
  420.         if (f != NULL)
  421.             return x;
  422.     }
  423.     return type_error("bad operand type(s) for -");
  424. }
  425.  
  426. PyObject *
  427. PyNumber_Multiply(PyObject *v, PyObject *w)
  428. {
  429.     PyTypeObject *tp = v->ob_type;
  430.     PySequenceMethods *m;
  431.  
  432.     BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply);
  433.     if (tp->tp_as_number != NULL &&
  434.         w->ob_type->tp_as_sequence != NULL &&
  435.         !PyInstance_Check(v)) {
  436.         /* number*sequence -- swap v and w */
  437.         PyObject *tmp = v;
  438.         v = w;
  439.         w = tmp;
  440.         tp = v->ob_type;
  441.     }
  442.     if (tp->tp_as_number != NULL) {
  443.         PyObject *x = NULL;
  444.         PyObject * (*f)(PyObject *, PyObject *);
  445.         if (PyInstance_Check(v)) {
  446.             /* Instances of user-defined classes get their
  447.                other argument uncoerced, so they may
  448.                implement sequence*number as well as
  449.                number*number. */
  450.             Py_INCREF(v);
  451.             Py_INCREF(w);
  452.         }
  453.         else if (PyNumber_Coerce(&v, &w) != 0)
  454.             return NULL;
  455.         if ((f = v->ob_type->tp_as_number->nb_multiply) != NULL)
  456.             x = (*f)(v, w);
  457.         Py_DECREF(v);
  458.         Py_DECREF(w);
  459.         if (f != NULL)
  460.             return x;
  461.     }
  462.     m = tp->tp_as_sequence;
  463.     if (m && m->sq_repeat) {
  464.         long mul_value;
  465.  
  466.         if (PyInt_Check(w)) {
  467.             mul_value = PyInt_AsLong(w);
  468.         }
  469.         else if (PyLong_Check(w)) {
  470.             mul_value = PyLong_AsLong(w);
  471.             if (mul_value == -1 && PyErr_Occurred())
  472.                                 return NULL; 
  473.         }
  474.         else {
  475.             return type_error(
  476.                 "can't multiply sequence with non-int");
  477.         }
  478.         return (*m->sq_repeat)(v, (int)mul_value);
  479.     }
  480.     return type_error("bad operand type(s) for *");
  481. }
  482.  
  483. PyObject *
  484. PyNumber_Divide(PyObject *v, PyObject *w)
  485. {
  486.     BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide);
  487.     if (v->ob_type->tp_as_number != NULL) {
  488.         PyObject *x = NULL;
  489.         PyObject * (*f)(PyObject *, PyObject *);
  490.         if (PyNumber_Coerce(&v, &w) != 0)
  491.             return NULL;
  492.         if ((f = v->ob_type->tp_as_number->nb_divide) != NULL)
  493.             x = (*f)(v, w);
  494.         Py_DECREF(v);
  495.         Py_DECREF(w);
  496.         if (f != NULL)
  497.             return x;
  498.     }
  499.     return type_error("bad operand type(s) for /");
  500. }
  501.  
  502. PyObject *
  503. PyNumber_Remainder(PyObject *v, PyObject *w)
  504. {
  505.     if (PyString_Check(v))
  506.         return PyString_Format(v, w);
  507.     else if (PyUnicode_Check(v))
  508.         return PyUnicode_Format(v, w);
  509.     BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder);
  510.     if (v->ob_type->tp_as_number != NULL) {
  511.         PyObject *x = NULL;
  512.         PyObject * (*f)(PyObject *, PyObject *);
  513.         if (PyNumber_Coerce(&v, &w) != 0)
  514.             return NULL;
  515.         if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL)
  516.             x = (*f)(v, w);
  517.         Py_DECREF(v);
  518.         Py_DECREF(w);
  519.         if (f != NULL)
  520.             return x;
  521.     }
  522.     return type_error("bad operand type(s) for %");
  523. }
  524.  
  525. PyObject *
  526. PyNumber_Divmod(PyObject *v, PyObject *w)
  527. {
  528.     BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod);
  529.     if (v->ob_type->tp_as_number != NULL) {
  530.         PyObject *x = NULL;
  531.         PyObject * (*f)(PyObject *, PyObject *);
  532.         if (PyNumber_Coerce(&v, &w) != 0)
  533.             return NULL;
  534.         if ((f = v->ob_type->tp_as_number->nb_divmod) != NULL)
  535.             x = (*f)(v, w);
  536.         Py_DECREF(v);
  537.         Py_DECREF(w);
  538.         if (f != NULL)
  539.             return x;
  540.     }
  541.     return type_error("bad operand type(s) for divmod()");
  542. }
  543.  
  544. /* Power (binary or ternary) */
  545.  
  546. static PyObject *
  547. do_pow(PyObject *v, PyObject *w)
  548. {
  549.     PyObject *res;
  550.     PyObject * (*f)(PyObject *, PyObject *, PyObject *);
  551.     BINOP(v, w, "__pow__", "__rpow__", do_pow);
  552.     if (v->ob_type->tp_as_number == NULL ||
  553.         w->ob_type->tp_as_number == NULL) {
  554.         PyErr_SetString(PyExc_TypeError,
  555.                 "pow(x, y) requires numeric arguments");
  556.         return NULL;
  557.     }
  558.     if (PyNumber_Coerce(&v, &w) != 0)
  559.         return NULL;
  560.     if ((f = v->ob_type->tp_as_number->nb_power) != NULL)
  561.         res = (*f)(v, w, Py_None);
  562.     else
  563.         res = type_error("pow(x, y) not defined for these operands");
  564.     Py_DECREF(v);
  565.     Py_DECREF(w);
  566.     return res;
  567. }
  568.  
  569. PyObject *
  570. PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
  571. {
  572.     PyObject *res;
  573.     PyObject *v1, *z1, *w2, *z2;
  574.     PyObject * (*f)(PyObject *, PyObject *, PyObject *);
  575.  
  576.     if (z == Py_None)
  577.         return do_pow(v, w);
  578.     /* XXX The ternary version doesn't do class instance coercions */
  579.     if (PyInstance_Check(v))
  580.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  581.     if (v->ob_type->tp_as_number == NULL ||
  582.         z->ob_type->tp_as_number == NULL ||
  583.         w->ob_type->tp_as_number == NULL) {
  584.         return type_error("pow(x, y, z) requires numeric arguments");
  585.     }
  586.     if (PyNumber_Coerce(&v, &w) != 0)
  587.         return NULL;
  588.     res = NULL;
  589.     v1 = v;
  590.     z1 = z;
  591.     if (PyNumber_Coerce(&v1, &z1) != 0)
  592.         goto error2;
  593.     w2 = w;
  594.     z2 = z1;
  595.      if (PyNumber_Coerce(&w2, &z2) != 0)
  596.         goto error1;
  597.     if ((f = v1->ob_type->tp_as_number->nb_power) != NULL)
  598.         res = (*f)(v1, w2, z2);
  599.     else
  600.         res = type_error(
  601.             "pow(x, y, z) not defined for these operands");
  602.     Py_DECREF(w2);
  603.     Py_DECREF(z2);
  604.   error1:
  605.     Py_DECREF(v1);
  606.     Py_DECREF(z1);
  607.   error2:
  608.     Py_DECREF(v);
  609.     Py_DECREF(w);
  610.     return res;
  611. }
  612.  
  613. /* Unary operators and functions */
  614.  
  615. PyObject *
  616. PyNumber_Negative(PyObject *o)
  617. {
  618.     PyNumberMethods *m;
  619.  
  620.     if (o == NULL)
  621.         return null_error();
  622.     m = o->ob_type->tp_as_number;
  623.     if (m && m->nb_negative)
  624.         return (*m->nb_negative)(o);
  625.  
  626.     return type_error("bad operand type for unary -");
  627. }
  628.  
  629. PyObject *
  630. PyNumber_Positive(PyObject *o)
  631. {
  632.     PyNumberMethods *m;
  633.  
  634.     if (o == NULL)
  635.         return null_error();
  636.     m = o->ob_type->tp_as_number;
  637.     if (m && m->nb_positive)
  638.         return (*m->nb_positive)(o);
  639.  
  640.     return type_error("bad operand type for unary +");
  641. }
  642.  
  643. PyObject *
  644. PyNumber_Invert(PyObject *o)
  645. {
  646.     PyNumberMethods *m;
  647.  
  648.     if (o == NULL)
  649.         return null_error();
  650.     m = o->ob_type->tp_as_number;
  651.     if (m && m->nb_invert)
  652.         return (*m->nb_invert)(o);
  653.  
  654.     return type_error("bad operand type for unary ~");
  655. }
  656.  
  657. PyObject *
  658. PyNumber_Absolute(PyObject *o)
  659. {
  660.     PyNumberMethods *m;
  661.  
  662.     if (o == NULL)
  663.         return null_error();
  664.     m = o->ob_type->tp_as_number;
  665.     if (m && m->nb_absolute)
  666.         return m->nb_absolute(o);
  667.  
  668.     return type_error("bad operand type for abs()");
  669. }
  670.  
  671. /* Add a check for embedded NULL-bytes in the argument. */
  672. static PyObject *
  673. int_from_string(const char *s, int len)
  674. {
  675.     char *end;
  676.     PyObject *x;
  677.  
  678.     x = PyInt_FromString((char*)s, &end, 10);
  679.     if (x == NULL)
  680.         return NULL;
  681.     if (end != s + len) {
  682.         PyErr_SetString(PyExc_ValueError,
  683.                 "null byte in argument for int()");
  684.         Py_DECREF(x);
  685.         return NULL;
  686.     }
  687.     return x;
  688. }
  689.  
  690. PyObject *
  691. PyNumber_Int(PyObject *o)
  692. {
  693.     PyNumberMethods *m;
  694.     const char *buffer;
  695.     int buffer_len;
  696.  
  697.     if (o == NULL)
  698.         return null_error();
  699.     if (PyInt_Check(o)) {
  700.         Py_INCREF(o);
  701.         return o;
  702.     }
  703.     if (PyString_Check(o))
  704.         return int_from_string(PyString_AS_STRING(o), 
  705.                        PyString_GET_SIZE(o));
  706.     if (PyUnicode_Check(o))
  707.         return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
  708.                      PyUnicode_GET_SIZE(o),
  709.                      10);
  710.     m = o->ob_type->tp_as_number;
  711.     if (m && m->nb_int)
  712.         return m->nb_int(o);
  713.     if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  714.         return int_from_string((char*)buffer, buffer_len);
  715.  
  716.     return type_error("object can't be converted to int");
  717. }
  718.  
  719. /* Add a check for embedded NULL-bytes in the argument. */
  720. static PyObject *
  721. long_from_string(const char *s, int len)
  722. {
  723.     char *end;
  724.     PyObject *x;
  725.  
  726.     x = PyLong_FromString((char*)s, &end, 10);
  727.     if (x == NULL)
  728.         return NULL;
  729.     if (end != s + len) {
  730.         PyErr_SetString(PyExc_ValueError,
  731.                 "null byte in argument for long()");
  732.         Py_DECREF(x);
  733.         return NULL;
  734.     }
  735.     return x;
  736. }
  737.  
  738. PyObject *
  739. PyNumber_Long(PyObject *o)
  740. {
  741.     PyNumberMethods *m;
  742.     const char *buffer;
  743.     int buffer_len;
  744.  
  745.     if (o == NULL)
  746.         return null_error();
  747.     if (PyLong_Check(o)) {
  748.         Py_INCREF(o);
  749.         return o;
  750.     }
  751.     if (PyString_Check(o))
  752.         /* need to do extra error checking that PyLong_FromString() 
  753.          * doesn't do.  In particular long('9.5') must raise an
  754.          * exception, not truncate the float.
  755.          */
  756.         return long_from_string(PyString_AS_STRING(o),
  757.                     PyString_GET_SIZE(o));
  758.     if (PyUnicode_Check(o))
  759.         /* The above check is done in PyLong_FromUnicode(). */
  760.         return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
  761.                       PyUnicode_GET_SIZE(o),
  762.                       10);
  763.     m = o->ob_type->tp_as_number;
  764.     if (m && m->nb_long)
  765.         return m->nb_long(o);
  766.     if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  767.         return long_from_string(buffer, buffer_len);
  768.  
  769.     return type_error("object can't be converted to long");
  770. }
  771.  
  772. PyObject *
  773. PyNumber_Float(PyObject *o)
  774. {
  775.     PyNumberMethods *m;
  776.  
  777.     if (o == NULL)
  778.         return null_error();
  779.     if (PyFloat_Check(o)) {
  780.         Py_INCREF(o);
  781.         return o;
  782.     }
  783.     if (!PyString_Check(o)) {
  784.         m = o->ob_type->tp_as_number;
  785.         if (m && m->nb_float)
  786.             return m->nb_float(o);
  787.     }
  788.     return PyFloat_FromString(o, NULL);
  789. }
  790.  
  791. /* Operations on sequences */
  792.  
  793. int
  794. PySequence_Check(PyObject *s)
  795. {
  796.     return s != NULL && s->ob_type->tp_as_sequence;
  797. }
  798.  
  799. int
  800. PySequence_Size(PyObject *s)
  801. {
  802.     PySequenceMethods *m;
  803.  
  804.     if (s == NULL) {
  805.         null_error();
  806.         return -1;
  807.     }
  808.  
  809.     m = s->ob_type->tp_as_sequence;
  810.     if (m && m->sq_length)
  811.         return m->sq_length(s);
  812.  
  813.     type_error("len() of unsized object");
  814.     return -1;
  815. }
  816.  
  817. #undef PySequence_Length
  818. int
  819. PySequence_Length(PyObject *s)
  820. {
  821.     return PySequence_Size(s);
  822. }
  823. #define PySequence_Length PySequence_Size
  824.  
  825. PyObject *
  826. PySequence_Concat(PyObject *s, PyObject *o)
  827. {
  828.     PySequenceMethods *m;
  829.  
  830.     if (s == NULL || o == NULL)
  831.         return null_error();
  832.  
  833.     m = s->ob_type->tp_as_sequence;
  834.     if (m && m->sq_concat)
  835.         return m->sq_concat(s, o);
  836.  
  837.     return type_error("object can't be concatenated");
  838. }
  839.  
  840. PyObject *
  841. PySequence_Repeat(PyObject *o, int count)
  842. {
  843.     PySequenceMethods *m;
  844.  
  845.     if (o == NULL)
  846.         return null_error();
  847.  
  848.     m = o->ob_type->tp_as_sequence;
  849.     if (m && m->sq_repeat)
  850.         return m->sq_repeat(o, count);
  851.  
  852.     return type_error("object can't be repeated");
  853. }
  854.  
  855. PyObject *
  856. PySequence_GetItem(PyObject *s, int i)
  857. {
  858.     PySequenceMethods *m;
  859.  
  860.     if (s == NULL)
  861.         return null_error();
  862.  
  863.     m = s->ob_type->tp_as_sequence;
  864.     if (m && m->sq_item) {
  865.         if (i < 0) {
  866.             if (m->sq_length) {
  867.                 int l = (*m->sq_length)(s);
  868.                 if (l < 0)
  869.                     return NULL;
  870.                 i += l;
  871.             }
  872.         }
  873.         return m->sq_item(s, i);
  874.     }
  875.  
  876.     return type_error("unindexable object");
  877. }
  878.  
  879. PyObject *
  880. PySequence_GetSlice(PyObject *s, int i1, int i2)
  881. {
  882.     PySequenceMethods *m;
  883.  
  884.     if (!s) return null_error();
  885.  
  886.     m = s->ob_type->tp_as_sequence;
  887.     if (m && m->sq_slice) {
  888.         if (i1 < 0 || i2 < 0) {
  889.             if (m->sq_length) {
  890.                 int l = (*m->sq_length)(s);
  891.                 if (l < 0)
  892.                     return NULL;
  893.                 if (i1 < 0)
  894.                     i1 += l;
  895.                 if (i2 < 0)
  896.                     i2 += l;
  897.             }
  898.         }
  899.         return m->sq_slice(s, i1, i2);
  900.     }
  901.  
  902.     return type_error("unsliceable object");
  903. }
  904.  
  905. int
  906. PySequence_SetItem(PyObject *s, int i, PyObject *o)
  907. {
  908.     PySequenceMethods *m;
  909.  
  910.     if (s == NULL) {
  911.         null_error();
  912.         return -1;
  913.     }
  914.  
  915.     m = s->ob_type->tp_as_sequence;
  916.     if (m && m->sq_ass_item) {
  917.         if (i < 0) {
  918.             if (m->sq_length) {
  919.                 int l = (*m->sq_length)(s);
  920.                 if (l < 0)
  921.                     return -1;
  922.                 i += l;
  923.             }
  924.         }
  925.         return m->sq_ass_item(s, i, o);
  926.     }
  927.  
  928.     type_error("object doesn't support item assignment");
  929.     return -1;
  930. }
  931.  
  932. int
  933. PySequence_DelItem(PyObject *s, int i)
  934. {
  935.     PySequenceMethods *m;
  936.  
  937.     if (s == NULL) {
  938.         null_error();
  939.         return -1;
  940.     }
  941.  
  942.     m = s->ob_type->tp_as_sequence;
  943.     if (m && m->sq_ass_item) {
  944.         if (i < 0) {
  945.             if (m->sq_length) {
  946.                 int l = (*m->sq_length)(s);
  947.                 if (l < 0)
  948.                     return -1;
  949.                 i += l;
  950.             }
  951.         }
  952.         return m->sq_ass_item(s, i, (PyObject *)NULL);
  953.     }
  954.  
  955.     type_error("object doesn't support item deletion");
  956.     return -1;
  957. }
  958.  
  959. int
  960. PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o)
  961. {
  962.     PySequenceMethods *m;
  963.  
  964.     if (s == NULL) {
  965.         null_error();
  966.         return -1;
  967.     }
  968.  
  969.     m = s->ob_type->tp_as_sequence;
  970.     if (m && m->sq_ass_slice) {
  971.         if (i1 < 0 || i2 < 0) {
  972.             if (m->sq_length) {
  973.                 int l = (*m->sq_length)(s);
  974.                 if (l < 0)
  975.                     return -1;
  976.                 if (i1 < 0)
  977.                     i1 += l;
  978.                 if (i2 < 0)
  979.                     i2 += l;
  980.             }
  981.         }
  982.         return m->sq_ass_slice(s, i1, i2, o);
  983.     }
  984.     type_error("object doesn't support slice assignment");
  985.     return -1;
  986. }
  987.  
  988. int
  989. PySequence_DelSlice(PyObject *s, int i1, int i2)
  990. {
  991.     PySequenceMethods *m;
  992.  
  993.     if (s == NULL) {
  994.         null_error();
  995.         return -1;
  996.     }
  997.  
  998.     m = s->ob_type->tp_as_sequence;
  999.     if (m && m->sq_ass_slice) {
  1000.         if (i1 < 0 || i2 < 0) {
  1001.             if (m->sq_length) {
  1002.                 int l = (*m->sq_length)(s);
  1003.                 if (l < 0)
  1004.                     return -1;
  1005.                 if (i1 < 0)
  1006.                     i1 += l;
  1007.                 if (i2 < 0)
  1008.                     i2 += l;
  1009.             }
  1010.         }
  1011.         return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
  1012.     }
  1013.     type_error("object doesn't support slice deletion");
  1014.     return -1;
  1015. }
  1016.  
  1017. PyObject *
  1018. PySequence_Tuple(PyObject *v)
  1019. {
  1020.     PySequenceMethods *m;
  1021.  
  1022.     if (v == NULL)
  1023.         return null_error();
  1024.  
  1025.     if (PyTuple_Check(v)) {
  1026.         Py_INCREF(v);
  1027.         return v;
  1028.     }
  1029.  
  1030.     if (PyList_Check(v))
  1031.         return PyList_AsTuple(v);
  1032.  
  1033.     /* There used to be code for strings here, but tuplifying strings is
  1034.        not a common activity, so I nuked it.  Down with code bloat! */
  1035.  
  1036.     /* Generic sequence object */
  1037.     m = v->ob_type->tp_as_sequence;
  1038.     if (m && m->sq_item) {
  1039.         int i;
  1040.         PyObject *t;
  1041.         int n = PySequence_Size(v);
  1042.         if (n < 0)
  1043.             return NULL;
  1044.         t = PyTuple_New(n);
  1045.         if (t == NULL)
  1046.             return NULL;
  1047.         for (i = 0; ; i++) {
  1048.             PyObject *item = (*m->sq_item)(v, i);
  1049.             if (item == NULL) {
  1050.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1051.                     PyErr_Clear();
  1052.                 else {
  1053.                     Py_DECREF(t);
  1054.                     t = NULL;
  1055.                 }
  1056.                 break;
  1057.             }
  1058.             if (i >= n) {
  1059.                 if (n < 500)
  1060.                     n += 10;
  1061.                 else
  1062.                     n += 100;
  1063.                 if (_PyTuple_Resize(&t, n, 0) != 0)
  1064.                     break;
  1065.             }
  1066.             PyTuple_SET_ITEM(t, i, item);
  1067.         }
  1068.         if (i < n && t != NULL)
  1069.             _PyTuple_Resize(&t, i, 0);
  1070.         return t;
  1071.     }
  1072.  
  1073.     /* None of the above */
  1074.     return type_error("tuple() argument must be a sequence");
  1075. }
  1076.  
  1077. PyObject *
  1078. PySequence_List(PyObject *v)
  1079. {
  1080.     PySequenceMethods *m;
  1081.  
  1082.     if (v == NULL)
  1083.         return null_error();
  1084.  
  1085.     if (PyList_Check(v))
  1086.         return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
  1087.  
  1088.     m = v->ob_type->tp_as_sequence;
  1089.     if (m && m->sq_item) {
  1090.         int i;
  1091.         PyObject *l;
  1092.         int n = PySequence_Size(v);
  1093.         if (n < 0)
  1094.             return NULL;
  1095.         l = PyList_New(n);
  1096.         if (l == NULL)
  1097.             return NULL;
  1098.         for (i = 0; ; i++) {
  1099.             PyObject *item = (*m->sq_item)(v, i);
  1100.             if (item == NULL) {
  1101.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1102.                     PyErr_Clear();
  1103.                 else {
  1104.                     Py_DECREF(l);
  1105.                     l = NULL;
  1106.                 }
  1107.                 break;
  1108.             }
  1109.             if (i < n)
  1110.                 PyList_SET_ITEM(l, i, item);
  1111.             else if (PyList_Append(l, item) < 0) {
  1112.                 Py_DECREF(l);
  1113.                 l = NULL;
  1114.                 break;
  1115.             }
  1116.         }
  1117.         if (i < n && l != NULL) {
  1118.             if (PyList_SetSlice(l, i, n, (PyObject *)NULL) != 0) {
  1119.                 Py_DECREF(l);
  1120.                 l = NULL;
  1121.             }
  1122.         }
  1123.         return l;
  1124.     }
  1125.     return type_error("list() argument must be a sequence");
  1126. }
  1127.  
  1128. PyObject *
  1129. PySequence_Fast(PyObject *v, const char *m)
  1130. {
  1131.     if (v == NULL)
  1132.         return null_error();
  1133.  
  1134.     if (PyList_Check(v) || PyTuple_Check(v)) {
  1135.         Py_INCREF(v);
  1136.         return v;
  1137.     }
  1138.  
  1139.     v = PySequence_Tuple(v);
  1140.     if (v == NULL && PyErr_ExceptionMatches(PyExc_TypeError))
  1141.         return type_error(m);
  1142.  
  1143.     return v;
  1144. }
  1145.  
  1146. int
  1147. PySequence_Count(PyObject *s, PyObject *o)
  1148. {
  1149.     int l, i, n, cmp, err;
  1150.     PyObject *item;
  1151.  
  1152.     if (s == NULL || o == NULL) {
  1153.         null_error();
  1154.         return -1;
  1155.     }
  1156.     
  1157.     l = PySequence_Size(s);
  1158.     if (l < 0)
  1159.         return -1;
  1160.  
  1161.     n = 0;
  1162.     for (i = 0; i < l; i++) {
  1163.         item = PySequence_GetItem(s, i);
  1164.         if (item == NULL)
  1165.             return -1;
  1166.         err = PyObject_Cmp(item, o, &cmp);
  1167.         Py_DECREF(item);
  1168.         if (err < 0)
  1169.             return err;
  1170.         if (cmp == 0)
  1171.             n++;
  1172.     }
  1173.     return n;
  1174. }
  1175.  
  1176. int
  1177. PySequence_Contains(PyObject *w, PyObject *v) /* v in w */
  1178. {
  1179.     int i, cmp;
  1180.     PyObject *x;
  1181.     PySequenceMethods *sq;
  1182.  
  1183.     if(PyType_HasFeature(w->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
  1184.         sq = w->ob_type->tp_as_sequence;
  1185.             if(sq != NULL && sq->sq_contains != NULL)
  1186.             return (*sq->sq_contains)(w, v);
  1187.     }
  1188.     
  1189.     /* If there is no better way to check whether an item is is contained,
  1190.        do it the hard way */
  1191.     sq = w->ob_type->tp_as_sequence;
  1192.     if (sq == NULL || sq->sq_item == NULL) {
  1193.         PyErr_SetString(PyExc_TypeError,
  1194.             "'in' or 'not in' needs sequence right argument");
  1195.         return -1;
  1196.     }
  1197.  
  1198.     for (i = 0; ; i++) {
  1199.         x = (*sq->sq_item)(w, i);
  1200.         if (x == NULL) {
  1201.             if (PyErr_ExceptionMatches(PyExc_IndexError)) {
  1202.                 PyErr_Clear();
  1203.                 break;
  1204.             }
  1205.             return -1;
  1206.         }
  1207.         cmp = PyObject_Compare(v, x);
  1208.         Py_XDECREF(x);
  1209.         if (cmp == 0)
  1210.             return 1;
  1211.         if (PyErr_Occurred())
  1212.             return -1;
  1213.     }
  1214.  
  1215.     return 0;
  1216. }
  1217.  
  1218. /* Backwards compatibility */
  1219. #undef PySequence_In
  1220. int
  1221. PySequence_In(PyObject *w, PyObject *v)
  1222. {
  1223.     return PySequence_Contains(w, v);
  1224. }
  1225.  
  1226. int
  1227. PySequence_Index(PyObject *s, PyObject *o)
  1228. {
  1229.     int l, i, cmp, err;
  1230.     PyObject *item;
  1231.  
  1232.     if (s == NULL || o == NULL) {
  1233.         null_error();
  1234.         return -1;
  1235.     }
  1236.     
  1237.     l = PySequence_Size(s);
  1238.     if (l < 0)
  1239.         return -1;
  1240.  
  1241.     for (i = 0; i < l; i++) {
  1242.         item = PySequence_GetItem(s, i);
  1243.         if (item == NULL)
  1244.             return -1;
  1245.         err = PyObject_Cmp(item, o, &cmp);
  1246.         Py_DECREF(item);
  1247.         if (err < 0)
  1248.             return err;
  1249.         if (cmp == 0)
  1250.             return i;
  1251.     }
  1252.  
  1253.     PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list");
  1254.     return -1;
  1255. }
  1256.  
  1257. /* Operations on mappings */
  1258.  
  1259. int
  1260. PyMapping_Check(PyObject *o)
  1261. {
  1262.     return o && o->ob_type->tp_as_mapping;
  1263. }
  1264.  
  1265. int
  1266. PyMapping_Size(PyObject *o)
  1267. {
  1268.     PyMappingMethods *m;
  1269.  
  1270.     if (o == NULL) {
  1271.         null_error();
  1272.         return -1;
  1273.     }
  1274.  
  1275.     m = o->ob_type->tp_as_mapping;
  1276.     if (m && m->mp_length)
  1277.         return m->mp_length(o);
  1278.  
  1279.     type_error("len() of unsized object");
  1280.     return -1;
  1281. }
  1282.  
  1283. #undef PyMapping_Length
  1284. int
  1285. PyMapping_Length(PyObject *o)
  1286. {
  1287.     return PyMapping_Size(o);
  1288. }
  1289. #define PyMapping_Length PyMapping_Size
  1290.  
  1291. PyObject *
  1292. PyMapping_GetItemString(PyObject *o, char *key)
  1293. {
  1294.     PyObject *okey, *r;
  1295.  
  1296.     if (key == NULL)
  1297.         return null_error();
  1298.  
  1299.     okey = PyString_FromString(key);
  1300.     if (okey == NULL)
  1301.         return NULL;
  1302.     r = PyObject_GetItem(o, okey);
  1303.     Py_DECREF(okey);
  1304.     return r;
  1305. }
  1306.  
  1307. int
  1308. PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
  1309. {
  1310.     PyObject *okey;
  1311.     int r;
  1312.  
  1313.     if (key == NULL) {
  1314.         null_error();
  1315.         return -1;
  1316.     }
  1317.  
  1318.     okey = PyString_FromString(key);
  1319.     if (okey == NULL)
  1320.         return -1;
  1321.     r = PyObject_SetItem(o, okey, value);
  1322.     Py_DECREF(okey);
  1323.     return r;
  1324. }
  1325.  
  1326. int
  1327. PyMapping_HasKeyString(PyObject *o, char *key)
  1328. {
  1329.     PyObject *v;
  1330.  
  1331.     v = PyMapping_GetItemString(o, key);
  1332.     if (v) {
  1333.         Py_DECREF(v);
  1334.         return 1;
  1335.     }
  1336.     PyErr_Clear();
  1337.     return 0;
  1338. }
  1339.  
  1340. int
  1341. PyMapping_HasKey(PyObject *o, PyObject *key)
  1342. {
  1343.     PyObject *v;
  1344.  
  1345.     v = PyObject_GetItem(o, key);
  1346.     if (v) {
  1347.         Py_DECREF(v);
  1348.         return 1;
  1349.     }
  1350.     PyErr_Clear();
  1351.     return 0;
  1352. }
  1353.  
  1354. /* Operations on callable objects */
  1355.  
  1356. /* XXX PyCallable_Check() is in object.c */
  1357.  
  1358. PyObject *
  1359. PyObject_CallObject(PyObject *o, PyObject *a)
  1360. {
  1361.     PyObject *r;
  1362.     PyObject *args = a;
  1363.  
  1364.     if (args == NULL) {
  1365.         args = PyTuple_New(0);
  1366.         if (args == NULL)
  1367.             return NULL;
  1368.     }
  1369.  
  1370.     r = PyEval_CallObject(o, args);
  1371.  
  1372.     if (args != a) {
  1373.         Py_DECREF(args);
  1374.     }
  1375.  
  1376.     return r;
  1377. }
  1378.  
  1379. PyObject *
  1380. PyObject_CallFunction(PyObject *callable, char *format, ...)
  1381. {
  1382.     va_list va;
  1383.     PyObject *args, *retval;
  1384.     va_start(va, format);
  1385.  
  1386.     if (callable == NULL) {
  1387.         va_end(va);
  1388.         return null_error();
  1389.     }
  1390.  
  1391.     if (format)
  1392.         args = Py_VaBuildValue(format, va);
  1393.     else
  1394.         args = PyTuple_New(0);
  1395.  
  1396.     va_end(va);
  1397.     
  1398.     if (args == NULL)
  1399.         return NULL;
  1400.  
  1401.     if (!PyTuple_Check(args)) {
  1402.         PyObject *a;
  1403.  
  1404.         a = PyTuple_New(1);
  1405.         if (a == NULL)
  1406.             return NULL;
  1407.         if (PyTuple_SetItem(a, 0, args) < 0)
  1408.             return NULL;
  1409.         args = a;
  1410.     }
  1411.     retval = PyObject_CallObject(callable, args);
  1412.  
  1413.     Py_DECREF(args);
  1414.  
  1415.     return retval;
  1416. }
  1417.  
  1418. PyObject *
  1419. PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
  1420. {
  1421.     va_list va;
  1422.     PyObject *args, *func = 0, *retval;
  1423.     va_start(va, format);
  1424.  
  1425.     if (o == NULL || name == NULL) {
  1426.         va_end(va);
  1427.         return null_error();
  1428.     }
  1429.  
  1430.     func = PyObject_GetAttrString(o, name);
  1431.     if (func == NULL) {
  1432.         va_end(va);
  1433.         PyErr_SetString(PyExc_AttributeError, name);
  1434.         return 0;
  1435.     }
  1436.  
  1437.     if (!PyCallable_Check(func)) {
  1438.         va_end(va);
  1439.         return type_error("call of non-callable attribute");
  1440.     }
  1441.  
  1442.     if (format && *format)
  1443.         args = Py_VaBuildValue(format, va);
  1444.     else
  1445.         args = PyTuple_New(0);
  1446.  
  1447.     va_end(va);
  1448.  
  1449.     if (!args)
  1450.         return NULL;
  1451.  
  1452.     if (!PyTuple_Check(args)) {
  1453.         PyObject *a;
  1454.  
  1455.         a = PyTuple_New(1);
  1456.         if (a == NULL)
  1457.             return NULL;
  1458.         if (PyTuple_SetItem(a, 0, args) < 0)
  1459.             return NULL;
  1460.         args = a;
  1461.     }
  1462.  
  1463.     retval = PyObject_CallObject(func, args);
  1464.  
  1465.     Py_DECREF(args);
  1466.     Py_DECREF(func);
  1467.  
  1468.     return retval;
  1469. }
  1470.